package com.princess.demok.base

import android.annotation.TargetApi
import android.app.ActivityManager
import android.content.Context
import android.os.Build
import android.os.Process
import androidx.multidex.MultiDex
import com.princess.baselib.base.BaseApplication
import com.princess.baselib.utils.LogUtils
import com.princess.demok.common.callback.EmptyCallBack
import com.princess.demok.common.callback.ErrorCallBack
import com.princess.demok.common.callback.LoadingCallBack
import com.princess.demok.net.data.HistoryData
import com.princess.demok.utils.Constant
import com.facebook.drawee.backends.pipeline.Fresco
import com.kingja.loadsir.core.LoadSir
import com.tencent.bugly.Bugly
import com.tencent.mmkv.MMKV
import dagger.hilt.android.HiltAndroidApp


/**
 * @author: xha
 * @date: 2020/9/28 14:40
 * @Description:
 */
@HiltAndroidApp
class MyApplication : BaseApplication() {

    var hisList: List<HistoryData> = arrayListOf()

    companion object {
        lateinit var mContext: MyApplication
        fun init(myApplication: MyApplication) {
            this.mContext = myApplication
        }
    }

    override fun onCreate() {
        super.onCreate()
        init(this)

        if (getCurProcessName() == applicationInfo.packageName) {
            setFresco()
            setBugly()
//            setJPush()
            setLoadSir()
            setMMKV()
            setOppo()
        }
    }

    private fun setOppo() {
//        SdkAgent.init(this);
    }

    private fun setMMKV() {
        val rootDir = MMKV.initialize(this)
        LogUtils.i("mmkv $rootDir")

    }

    private fun setLoadSir() {
        LoadSir.beginBuilder()
            .addCallback(ErrorCallBack())
            .addCallback(LoadingCallBack())
            .addCallback(EmptyCallBack())
            .commit()
    }

//    private fun setJPush() {
//        JPushInterface.setDebugMode(com.princess.baselib.BuildConfig.DEBUG)
//        JPushInterface.init(this)
//
//    }

    private fun setBugly() {
        Bugly.init(applicationContext, Constant.BUGLY_APPID, com.princess.baselib.BuildConfig.DEBUG);
    }


    private fun setFresco() {
        Fresco.initialize(this);
//        val config: ImagePipelineConfig = ImageLoaderConfig.getImagePipelineConfig(this)
//        Fresco.initialize(this, config)
    }


    @TargetApi(Build.VERSION_CODES.CUPCAKE)
    fun getCurProcessName(): String? {
        val pid = Process.myPid()
        val activityManager = getSystemService(ACTIVITY_SERVICE) as ActivityManager
        val list = activityManager.runningAppProcesses
        if (list != null) {
            for (runningAppProcessInfo in list) {
                if (runningAppProcessInfo.pid == pid) {
                    return runningAppProcessInfo.processName
                }
            }
        }
        return ""
    }


    override fun attachBaseContext(base: Context?) {
        super.attachBaseContext(base)
        MultiDex.install(this)


    }
}